﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using ServiceGerenciador.Model;

public partial class paginas_Fornecedores : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {            
            IList forneprod = new ArrayList();            
            forneprod = ServiceGerenciador.Model.ForneProd.FindAll().ToArray();
            
           // var forne = ServiceGerenciador.Model.ForneProd.FindAll().ToArray();

            if (forneprod.Count > 0)
                mvCrud.ActiveViewIndex = 0;//controle se existe registro e mostra a view
            else
                mvCrud.ActiveViewIndex = 1;

            gvCrud.DataSource = forneprod;
            gvCrud.DataBind();

    }
            //if (!IsPostBack)
            //{
            //    var forneprod = ServiceGerenciador.Model.ForneProd.FindAll();

            //    if (forneprod.Length > 0)
            //        mvCrud.ActiveViewIndex = 0;//controle se existe registro e mostra a view
            //    else
            //        mvCrud.ActiveViewIndex = 1;

            //    gvCrud.DataSource = forneprod;
            //    gvCrud.DataBind();
            //}

    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
        Response.Redirect("FornecedorNovo.aspx");
    }
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
         if (string.IsNullOrEmpty(txtTermo.Text))
            return;
         
        //var teste = ServiceGerenciador.Model.Forne.Pesquisa(txtTermo.Text).FirstOrDefault();

        // if (teste == null)
        // {
        //     lblPesquisa.Text = "Fornecedor inexistente!";             
        // }
        // else
        // {
        //     lblPesquisa.Text = "";
             gvCrud.DataSource = ServiceGerenciador.Model.Forne.Pesquisa(txtTermo.Text);              
        // }       
        gvCrud.DataBind();
    }
    protected void btnLimpar_Click(object sender, EventArgs e)
    {
        Response.Redirect("Fornecedores.aspx");
    }
}
